package heatmap
import (
"fmt"
"github.com/K-Phoen/grabana/errors"
"github.com/K-Phoen/grabana/heatmap/axis"
"github.com/K-Phoen/grabana/links"
"github.com/K-Phoen/grabana/target/graphite"
"github.com/K-Phoen/grabana/target/influxdb"
"github.com/K-Phoen/grabana/target/prometheus"
"github.com/K-Phoen/grabana/target/stackdriver"
"github.com/K-Phoen/sdk"
)
type DataFormatMode string
const (
TimeSeriesBuckets DataFormatMode = "tsbuckets"
TimeSeries DataFormatMode = "timeseries"
)
type LegendOption uint16
const (
Hide LegendOption = iota
)
type Option func (stat *Heatmap ) error
type Heatmap struct {
Builder *sdk .Panel
}
func New (title string , options ...Option ) (*Heatmap , error ) {
panel := &Heatmap {Builder : sdk .NewHeatmap (title )}
panel .Builder .IsNew = false
panel .Builder .HeatmapPanel .Cards = struct {
CardPadding *float64 `json:"cardPadding"`
CardRound *float64 `json:"cardRound"`
}{}
panel .Builder .HeatmapPanel .Color = struct {
CardColor string `json:"cardColor"`
ColorScale string `json:"colorScale"`
ColorScheme string `json:"colorScheme"`
Exponent float64 `json:"exponent"`
Min *float64 `json:"min,omitempty"`
Max *float64 `json:"max,omitempty"`
Mode string `json:"mode"`
}{
CardColor : "#b4ff00" ,
ColorScale : "sqrt" ,
ColorScheme : "interpolateSpectral" ,
Exponent : 0.5 ,
Mode : "spectrum" ,
}
panel .Builder .HeatmapPanel .Legend = struct {
Show bool `json:"show"`
}{
Show : true ,
}
panel .Builder .HeatmapPanel .Tooltip = struct {
Show bool `json:"show"`
ShowHistogram bool `json:"showHistogram"`
}{
Show : true ,
ShowHistogram : true ,
}
panel .Builder .HeatmapPanel .XAxis = struct {
Show bool `json:"show"`
}{
Show : true ,
}
panel .Builder .HeatmapPanel .YBucketBound = "auto"
for _ , opt := range append (defaults (), options ...) {
if err := opt (panel ); err != nil {
return nil , err
}
}
return panel , nil
}
func defaults() []Option {
return []Option {
Span (6 ),
DataFormat (TimeSeriesBuckets ),
HideZeroBuckets (),
HighlightCards (),
defaultYAxis (),
}
}
func defaultYAxis() Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .HeatmapPanel .YAxis = *axis .New ().Builder
return nil
}
}
func Links (panelLinks ...links .Link ) Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .Links = make ([]sdk .Link , 0 , len (panelLinks ))
for _ , link := range panelLinks {
heatmap .Builder .Links = append (heatmap .Builder .Links , link .Builder )
}
return nil
}
}
func DataSource (source string ) Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .Datasource = &sdk .DatasourceRef {LegacyName : source }
return nil
}
}
func DataFormat (format DataFormatMode ) Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .HeatmapPanel .DataFormat = string (format )
return nil
}
}
func WithPrometheusTarget (query string , options ...prometheus .Option ) Option {
target := prometheus .New (query , options ...)
return func (heatmap *Heatmap ) error {
heatmap .Builder .AddTarget (&sdk .Target {
RefID : target .Ref ,
Hide : target .Hidden ,
Expr : target .Expr ,
IntervalFactor : target .IntervalFactor ,
Interval : target .Interval ,
Step : target .Step ,
LegendFormat : target .LegendFormat ,
Instant : target .Instant ,
Format : target .Format ,
})
return nil
}
}
func WithGraphiteTarget (query string , options ...graphite .Option ) Option {
target := graphite .New (query , options ...)
return func (heatmap *Heatmap ) error {
heatmap .Builder .AddTarget (target .Builder )
return nil
}
}
func WithInfluxDBTarget (query string , options ...influxdb .Option ) Option {
target := influxdb .New (query , options ...)
return func (heatmap *Heatmap ) error {
heatmap .Builder .AddTarget (target .Builder )
return nil
}
}
func WithStackdriverTarget (target *stackdriver .Stackdriver ) Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .AddTarget (target .Builder )
return nil
}
}
func Span (span float32 ) Option {
return func (heatmap *Heatmap ) error {
if span < 1 || span > 12 {
return fmt .Errorf ("span must be between 1 and 12: %w" , errors .ErrInvalidArgument )
}
heatmap .Builder .Span = span
return nil
}
}
func Height (height string ) Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .Height = &height
return nil
}
}
func Description (content string ) Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .Description = &content
return nil
}
}
func Transparent () Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .Transparent = true
return nil
}
}
func Legend (opts ...LegendOption ) Option {
return func (heatmap *Heatmap ) error {
for _ , opt := range opts {
if opt == Hide {
heatmap .Builder .HeatmapPanel .Legend .Show = false
}
}
return nil
}
}
func ShowZeroBuckets () Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .HeatmapPanel .HideZeroBuckets = false
return nil
}
}
func HideZeroBuckets () Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .HeatmapPanel .HideZeroBuckets = true
return nil
}
}
func HighlightCards () Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .HeatmapPanel .HighlightCards = true
return nil
}
}
func NoHighlightCards () Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .HeatmapPanel .HighlightCards = false
return nil
}
}
func ReverseYBuckets () Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .HeatmapPanel .ReverseYBuckets = true
return nil
}
}
func HideTooltip () Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .HeatmapPanel .Tooltip .Show = false
return nil
}
}
func HideTooltipHistogram () Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .HeatmapPanel .Tooltip .ShowHistogram = false
return nil
}
}
func TooltipDecimals (decimals int ) Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .HeatmapPanel .TooltipDecimals = decimals
return nil
}
}
func HideXAxis () Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .HeatmapPanel .XAxis .Show = false
return nil
}
}
func YAxis (opts ...axis .Option ) Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .HeatmapPanel .YAxis = *axis .New (opts ...).Builder
return nil
}
}
func Repeat (repeat string ) Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .Repeat = &repeat
return nil
}
}
func RepeatDirection (direction sdk .RepeatDirection ) Option {
return func (heatmap *Heatmap ) error {
heatmap .Builder .RepeatDirection = &direction
return nil
}
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .